import Fileutils
import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade
import System.Directory
import Graphics.UI.Gtk.Gdk.EventM
import Control.Monad.Trans
import System.Glib.Types
import Paths_vncconfiggui

getGladeFilePath :: IO String
getGladeFilePath = do
    -- Check the existence of bin directory
    homeDirPath <- getDataDir
    return (homeDirPath ++ "/data/vncconfig.glade")

main :: IO ()
main = do
    initGUI
    gladeFilePath <- getGladeFilePath
    jGladeXml <- xmlNew gladeFilePath
    case jGladeXml of
        Just gladeXML -> do
                setupMenuHandlers gladeXML
                setupComboBoxHandlers
                populateComboBox gladeXML
                --  Set combobox handler
                setActivateHandler gladeXML
                mainWindow <- xmlGetWidget gladeXML castToWindow "vncMainWindow"
                onDestroy mainWindow mainQuit
                widgetShowAll mainWindow
                mainGUI
        Nothing -> putStrLn "Parsing of glade XML failed"

setupMenuHandlers :: GladeXML -> IO ()
setupMenuHandlers gladeXML = do
    quitMenuItem <- xmlGetWidget gladeXML castToMenuItem "FileQuitItem"
    quitMenuItem `onActivateLeaf` (mainQuit)
    return ()

setupComboBoxHandlers :: IO ()
setupComboBoxHandlers = return ()

setActivateHandler :: GladeXML -> IO ()
setActivateHandler gladeXML = do
    comboBoxEntry <- xmlGetWidget gladeXML castToComboBoxEntry "comboboxentry1"
    configStringTextEntry <- xmlGetWidget gladeXML castToEntry "configStringEntry"
    (Just w) <- binGetChild comboBoxEntry
    let entry = castToEntry w
    onEntryActivate  entry $ do
        str <- entryGetText entry
        putStrLn ("Debug : Activated with string" ++ str)
    changeConnectId <- on comboBoxEntry changed $ do
           maybeStr <- comboBoxEntryGetActiveText comboBoxEntry
           case maybeStr of
             (Just str) -> do
                             putStrLn ("Debug : Text in comboBox is " ++ str)
                             vncservers <- getVncServers
                             let configString = getConfigString vncservers str
                             entrySetText  configStringTextEntry configString
                             return ()
             Nothing -> return ()
    on entry focusInEvent $do
        updateSignal True changeConnectId
        --disconnect the signal
    on entry focusOutEvent $do
        --reconnect the signal
        updateSignal False changeConnectId
    return ()

updateSignal :: GObjectClass object =>Bool-> ConnectId object -> EventM EFocus Bool
updateSignal True changeConnectId  = do
    liftIO $ do
    signalBlock changeConnectId
    return False

updateSignal False changeConnectId  = do
    liftIO $do
    signalUnblock changeConnectId
    return False

populateComboBox :: GladeXML -> IO ()
populateComboBox gladeXML = do
    comboBoxEntry <- xmlGetWidget gladeXML castToComboBoxEntry "comboboxentry1"
    servers <- getVncServers
    store<- comboBoxEntrySetModelText comboBoxEntry
    mapM (putStrLn.show) servers
    -- Need to use a list store for the combo box
    mapM_ (comboBoxAppendText comboBoxEntry) [((show.getPort) x) ++ (getUser x) |x<- servers]
    return ()

